package com.drama.outtake.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drama.outtake.dao.StarMapper;
import com.drama.outtake.domain.Star;
import com.drama.outtake.service.IStarService;
import com.drama.outtake.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : 李先均
 * @Date 2023/3/26 23:42
 */
@Service
@RequiredArgsConstructor
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements IStarService {
    private final IUserService userService;

    @Override
    public Boolean isStar(Long userId, Long shopId) {
        return !baseMapper.selectList(new QueryWrapper<Star>()
                        .eq("user_id", userId)
                        .eq("shop_id", shopId))
                .isEmpty();
    }

    @Override
    public void updateStar(Long shopId, Boolean star) {
        Long userId = userService.getMyUserId();
        if (Boolean.TRUE.equals(star)) {
            boolean exist = baseMapper.selectList(new QueryWrapper<Star>()
                    .eq("shop_id", shopId)
                    .eq("user_id", userId)).isEmpty();
            if (exist) {
                baseMapper.insert(new Star(null, userId, shopId));
            }
        } else {
            baseMapper.delete(new QueryWrapper<Star>()
                    .eq("shop_id", shopId)
                    .eq("user_id", userId));
        }
    }

    @Override
    public List<Long> getStarShopIds() {
        return baseMapper.selectList(new QueryWrapper<Star>().eq("user_id", userService.getMyUserId())).stream()
                .map(Star::getShopId)
                .collect(Collectors.toList());
    }
}
